/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var countNodes = function (root) {
    if (root === null) return 0
    return getCount(root)
};

function getCount(root) {
    if (root === null) {
        return 0
    }
    return getCount(root.left) + getCount(root.right) + 1
}


/**
 * @param {TreeNode} root
 * @return {number}
 */
var countNodes = function (root) {
    if(root === null) return 0
    let res = 0,
    queue = [root]

    while(queue.length>0){
        let len= queue.length,
        node;

        for(let i = 0;i<len;i++){
            node = queue.shift()
            res++
            node.left && queue.push(node.left)
            node.right && queue.push(node.right)
        }
    }

    return res
};